using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;

namespace Acms.DataControls
{
    public class AcmsSiteMapProvider : System.Web.SiteMapProvider
    {
        public AcmsSiteMapProvider()
        {
        }

        private string menuName;
        protected string MenuName
        {
            get { return menuName; }
            set { menuName = value; }
        }

        private int siteId;
        protected int SiteId
        {
            get { return siteId; }
            set { siteId = value; }
        }

        private string rootText;
        protected string RootText
        {
            get { return rootText; }
            set { rootText = value; }
        }

        private string rootUrl;
        public string RootUrl
        {
            get { return rootUrl; }
            set { rootUrl = value; }
        }

        private int menuId;
        protected int MenuId
        {
            get 
            {
                if (menuId == 0 && !string.IsNullOrEmpty(MenuName) && SiteId > 0)
                {
                    Bll.Menu menu = Bll.Menu.GetByNameAndSiteId(MenuName, SiteId);
                    if (menu != null)
                    {
                        menuId = menu.Id; 
                    }
                }
                return menuId;
            }
        }


        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection attributes)
        {
            base.Initialize(name, attributes);
            try
            {
                siteId = int.Parse(attributes["SiteId"]);
            }
            catch
            {
                siteId = 1;
            }
            menuName = attributes["MenuName"];
            rootText = attributes["RootText"];
            rootUrl = attributes["RootUrl"];
        }

        public override System.Web.SiteMapProvider RootProvider
        {
            get
            {
                return base.RootProvider;
            }
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            SiteMapNode node = new SiteMapNode(this, "RootText");
            if (!string.IsNullOrEmpty(RootText))
            {
                node.Title = RootText;
            }
            return node;
        }


        public override SiteMapNode FindSiteMapNode(string rawUrl)
        {
            return null;
            //throw new Exception("The method or operation is not implemented.");
        }

        public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
        {
            if (node.Key == "RootText")
            {
                return GetChildrenFromUrl(RootUrl);
            }
            else
            {
                return new SiteMapNodeCollection();
            }
            //throw new Exception("The method or operation is not implemented.");
        }

        public override SiteMapNode GetParentNode(SiteMapNode node)
        {
            return null;
            //throw new Exception("The method or operation is not implemented.");
        }

        private SiteMapNodeCollection GetChildrenFromUrl(string parentUrl)
        {
            List<Bll.Post> sections = Bll.Post.GetAllBySiteIdMenuIdAndParentUrl(SiteId, MenuId, parentUrl);
            SiteMapNodeCollection collection = new SiteMapNodeCollection();
            if (sections != null)
            {
                foreach (Bll.Post section in sections)
                {
                    collection.Add(Convert(section));
                }
            }
            return collection;
        }

        private SiteMapNode Convert(Bll.Post section)
        {
            SiteMapNode node = new SiteMapNode(this, section.Id.ToString());
            node.Description = section.DisplayName;
            node.Title = section.DisplayName;
            node.Url = section.Url;
            return node;
        }
    }
}
